package com.fingard.dsp.bank.directbank.cmb01;

import com.fingard.dsp.bank.directbank.bankInterface.IRetailStockMsg;
import com.fingard.dsp.bank.reqrespobj.retailstockmsg.ReqRetailStockMsgQry;
import com.fingard.dsp.bank.reqrespobj.retailstockmsg.RetAtsRetailStockMsg;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2019/9/21.
 */
public class CMB01RetailStockMsg extends CMB01Base implements IRetailStockMsg {
    @Override
    public RetAtsRetailStockMsg getRetailStockMsg(ReqRetailStockMsgQry qry) throws Exception {
        RetAtsRetailStockMsg retAts = new RetAtsRetailStockMsg(reqHeader);
        retAts.writeStart();
        try {
            CMB01Header header = new CMB01Header();
            header.FUNNAM = "TDAFLKRV";
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, header);
//            输入	TDAFLKRVX	输入属性	单记录
            xmlWriter.writeStartElement("TDAFLKRVX");
//            直连对接企业系统客户号	COREID	C(10)	否	长安s主机厂对应的10位客户号
            xmlWriter.writeElementString("COREID",bankFront.org );
//            业务参考号	YURREF	C(30)	否

            xmlWriter.writeElementString("YURREF", header.FUNNAM+getYURREF(reqHeader.transSeqID));
//            保留字段	REV50L	C(50)	可
            xmlWriter.writeElementString("REV50L", "");
            xmlWriter.writeEndElement();
            while (qry.readRecord()) {
//                输入	TDAFLKRVY	输入属性	多记录
                xmlWriter.writeStartElement("TDAFLKRVY");
//                经销商代码	DLENUM	C(40)	否
                xmlWriter.writeElementString("DLENUM",qry.ReqDetail.DealerCode);
//                经销商名称	DLENAM	Z(100)	否	经销商名称
                xmlWriter.writeElementString("DLENAM",qry.ReqDetail.DealerName);
//                年度累计零售车辆总数	YCRTOL	N	否
                xmlWriter.writeElementString("YCRTOL",qry.ReqDetail.Ndljls);
//                年度累计零售车辆总金额	YCRAMT	M	否
                xmlWriter.writeElementString("YCRAMT",qry.ReqDetail.NdljlsMoney);
//                本月累计零售车辆总数	MCRTOL	N	否
                xmlWriter.writeElementString("MCRTOL",qry.ReqDetail.Byljls);
//                本月累计零售车辆总金额	MCRAMT	M	否
                xmlWriter.writeElementString("MCRAMT",qry.ReqDetail.ByljlsMoney);
//                当日零售车辆总数	DCRTOL	N	否
                xmlWriter.writeElementString("DCRTOL",qry.ReqDetail.Drlscl);
//                当日零售车辆总金额	DCRAMT	M	否
                xmlWriter.writeElementString("DCRAMT",qry.ReqDetail.DrslclMoney);
//                实时库存车辆总数	TCRTOL	N	否
                xmlWriter.writeElementString("TCRTOL",qry.ReqDetail.Ssckcl);
//                实时库存车辆总金额	TCRAMT	M	否
                xmlWriter.writeElementString("TCRAMT",qry.ReqDetail.SsckclMoney);
                xmlWriter.writeEndElement();
            }
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            if(tmpStrRet[0].length() == 0) {
                StringBuilder tmpbkMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpbkMsg);
                if (tmpBkReader != null) {
                    String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                    if("0".equals(bankRetCode)) {
//                        TDAFLKRVZ
//                        返回代码	RTNCOD	C(7)	否	返回值：SCDS000则代表成功 其他值则代表失败
//                        返回信息	RTNMSG	Z(256)	可
                        String rtnCod = tmpBkReader.getFirstText("/CMBSDKPGK/TDAFLKRVZ/RTNCOD");
                        String rtnMsg = tmpBkReader.getFirstText("/CMBSDKPGK/TDAFLKRVZ/RTNMSG");
                        if("SCDS000".equals(rtnCod)) {
                            retAts.respCode ="0";
                            retAts.respInfo = rtnMsg;
                        }else{
                            throw new Exception(rtnCod+":"+rtnMsg);
                        }
                    }else{
                        String bankRetMsg =tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                        throw new Exception(bankRetCode+":"+bankRetMsg);
                    }
                }else {
                    throw new Exception(tmpBkReader.toString());
                }
            } else{
                throw new Exception(tmpStrRet[0]);
            }
        }catch (Exception ex){
            ex.printStackTrace();
            retAts.respCode ="-1";
            retAts.respInfo = ex.getMessage();
        }
        retAts.writeRespCode(retAts.respCode);
        retAts.writeRespInfo(retAts.respInfo);
        retAts.writeEnd();
        return retAts;
    }
}
